﻿using System;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace MulticastTV
{
    class MainApplication
    {
        [STAThread]
         public static void Main()
        {
            bool ok;
            Mutex m = new System.Threading.Mutex(true, "MulticastTVMutex", out ok);

            if (!ok)
            {
                /*
                 TODO:
                 Current process should not be killed, but somehow messaged to show main form.
                 It apperantly can't be done with windows messages (NotifyIcon class),
                 so we will have to use sockets.
                */
                
                 Process[] aProc = Process.GetProcessesByName("multicasttv");
                 Process currentp = Process.GetCurrentProcess();
                 foreach (Process p in aProc)
                 { 
                     if (!p.Equals(currentp))
                    {
                       p.Kill();
                       p.WaitForExit();
                       break;
                      }
                 }
            }
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           
            string[] arguments = Environment.GetCommandLineArgs();

            if (arguments.Length == 1)
            {
                StandBy.Start(false);
            }
            else {
                StandBy.Start(true);
            }

            Application.Run();
            GC.KeepAlive(m);    
        }
   }
}
